package com.example.controller;

import com.example.entity.pojo.Ordr;
import com.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }





    /**
     * 获取订单
     */
    @GetMapping("/{id}")
    public ResponseEntity<Ordr> getOrder(@PathVariable Long id) {
        Ordr order = orderService.getOrder(id);
        if (order != null) {
            return ResponseEntity.ok(order);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 保存订单
     */
    @PostMapping
    public ResponseEntity<Void> saveOrder(@RequestBody Ordr order) {
        orderService.saveOrder(order);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新订单
     */
    @PutMapping
    public ResponseEntity<Void> updateOrder(@RequestBody Ordr order) {
        orderService.updateOrder(order);
        return ResponseEntity.noContent().build();
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteOrder(@PathVariable Long id) {
        orderService.deleteOrder(id);
        return ResponseEntity.noContent().build();
    }






    /**
     * 从Redis获取订单
     */
    @GetMapping("/redis/{id}")
    public ResponseEntity<Ordr> getOrderWithRedis(@PathVariable Long id) {
        Ordr order = orderService.getOrderWithRedis(id);
        if (order != null) {
            return ResponseEntity.ok(order);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 保存订单到Redis
     */
    @PostMapping("/redis")
    public ResponseEntity<Void> saveOrderWithRedis(@RequestBody Ordr order) {
        orderService.saveOrderWithRedis(order);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新Redis中的订单
     */
    @PutMapping("/redis")
    public ResponseEntity<Void> updateOrderWithRedis(@RequestBody Ordr order) {
        orderService.updateOrderWithRedis(order);
        return ResponseEntity.noContent().build();
    }

    /**
     * 从Redis删除订单
     */
    @DeleteMapping("/redis/{id}")
    public ResponseEntity<Void> deleteOrderWithRedis(@PathVariable Long id) {
        orderService.deleteOrderWithRedis(id);
        return ResponseEntity.noContent().build();
    }
}
